//
//  AppDelegate.m
//  TodaysPlan
//
//  Created by terada atsushi on 11/09/19.
//  Copyright (c) 2011年 __MyCompanyName__. All rights reserved.
//

#import "AppDelegate.h"
#import "TodaysPlanSqliteDataSource.h"
#import "SqliteDB.h"
#import "TodaysPlanDataModel.h"
#import "Kal.h"
#import "ConfigManager.h"

@implementation AppDelegate

@synthesize window;

- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
{
    DLog(@"DEBUG start");

    ConfigManager *config = [[[ConfigManager alloc] init] autorelease];
    int i = [[config.cfgAPNS getValueString] intValue];
    if (i)
    {
        SqliteDB *db = [[[SqliteDB alloc] init] autorelease];
        [db initializeDatabaseIfNeeded];
        NSDateFormatter *createDateFormatter = [[[NSDateFormatter alloc] init] autorelease];
        [createDateFormatter setDateFormat:@"yyyy-MM-dd"];
        NSString* registDateStr = [createDateFormatter stringFromDate:[NSDate date]];

        int count = [TodaysPlanDataModel countTodaysPlan:registDateStr];
        [UIApplication sharedApplication].applicationIconBadgeNumber = count;
    }
    else{
        [UIApplication sharedApplication].applicationIconBadgeNumber = 0;        
    }
    self.window = [[[UIWindow alloc] initWithFrame:[[UIScreen mainScreen] bounds]] autorelease];
    
    /*
     *    Kal Initialization
     */
    kal = [[[KalViewController alloc] init] autorelease];
    kal.title = NSLocalizedString(@"rootTitle", "rootTitle is top page");
/*    
    //ナビゲーションバーにボタンを追加
    UIBarButtonItem *addButton = [[UIBarButtonItem alloc]
                                  initWithBarButtonSystemItem:UIBarButtonSystemItemCompose
                                  target:self
                                  action:@selector(addTodaysPlan:)
                                  ];
    kal.navigationItem.rightBarButtonItem = addButton;
*/
    kal.delegate = self;
    dataSource = [[TodaysPlanSqliteDataSource alloc] init];
    kal.dataSource = dataSource;
    
    // Setup the navigation stack and display it.
    navController = [[UINavigationController alloc] initWithRootViewController:kal];
    [window addSubview:navController.view];
    [navController.navigationBar setBarStyle:UIBarStyleBlackOpaque];
    [self.window makeKeyAndVisible];
    
    return YES;
}
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    TodaysPlan *todaysplan = [dataSource holidayAtIndexPath:indexPath];
    TodaysAddViewController *vc = [[[TodaysAddViewController alloc] initWithTodaysPlan:todaysplan] autorelease];
    vc.selectDate=todaysplan.date;
    [navController pushViewController:vc animated:YES];
}
- (void)tableView:(UITableView *)tableView willDisplayCell:(UITableViewCell *)cell forRowAtIndexPath:(NSIndexPath *)indexPath {
    TodaysPlan *todaysplan = [dataSource holidayAtIndexPath:indexPath];
    UIColor *aColor = [UIColor whiteColor];
    //DLog(@"priority=%@", todaysplan.priority);

    //todo refactoring
    if ([[todaysplan priority]intValue] == 0){
        ConfigManager *config = [[[ConfigManager alloc] init] autorelease];
        int i = [[config.cfgPriority_L getValueString] intValue];
        switch (i) {
            case 0:
                aColor = [UIColor colorWithRed:1.0 green:0.0 blue:0.0 alpha:1.0];
                break;
            case 1:
                aColor = [UIColor colorWithRed:0.0 green:0.0 blue:1.0 alpha:1.0];
                break;
            case 2:
                aColor = [UIColor colorWithRed:0.0 green:1.0 blue:0.0 alpha:1.0];
                break;
                
            default:
                break;
        }
    }
    else if ([[todaysplan priority]intValue] == 1){
        ConfigManager *config = [[[ConfigManager alloc] init] autorelease];
        int i = [[config.cfgPriority_M getValueString] intValue];
        switch (i) {
            case 0:
                aColor = [UIColor colorWithRed:1.0 green:0.0 blue:0.0 alpha:1.0];
                break;
            case 1:
                aColor = [UIColor colorWithRed:0.0 green:0.0 blue:1.0 alpha:1.0];
                break;
            case 2:
                aColor = [UIColor colorWithRed:0.0 green:1.0 blue:0.0 alpha:1.0];
                break;
                
            default:
                break;
        }
    }
    else if ([[todaysplan priority]intValue] == 2){
        ConfigManager *config = [[[ConfigManager alloc] init] autorelease];
        int i = [[config.cfgPriority_H getValueString] intValue];
        switch (i) {
            case 0:
                aColor = [UIColor colorWithRed:1.0 green:0.0 blue:0.0 alpha:1.0];
                break;
            case 1:
                aColor = [UIColor colorWithRed:0.0 green:0.0 blue:1.0 alpha:1.0];
                break;
            case 2:
                aColor = [UIColor colorWithRed:0.0 green:1.0 blue:0.0 alpha:1.0];
                break;
                
            default:
                break;
        }
    }
    cell.backgroundColor = aColor;
}
- (void)applicationWillResignActive:(UIApplication *)application
{
    /*
     Sent when the application is about to move from active to inactive state. This can occur for certain types of temporary interruptions (such as an incoming phone call or SMS message) or when the user quits the application and it begins the transition to the background state.
     Use this method to pause ongoing tasks, disable timers, and throttle down OpenGL ES frame rates. Games should use this method to pause the game.
     */
}

- (void)applicationDidEnterBackground:(UIApplication *)application
{
    ConfigManager *config = [[[ConfigManager alloc] init] autorelease];
    int i = [[config.cfgAPNS getValueString] intValue];
    if (i)
    {

        SqliteDB *db = [[[SqliteDB alloc] init] autorelease];
        [db initializeDatabaseIfNeeded];
        NSDateFormatter *createDateFormatter = [[[NSDateFormatter alloc] init] autorelease];
        [createDateFormatter setDateFormat:@"yyyy-MM-dd"];
        NSString* registDateStr = [createDateFormatter stringFromDate:[NSDate date]];
    
        int count = [TodaysPlanDataModel countTodaysPlan:registDateStr];
        [UIApplication sharedApplication].applicationIconBadgeNumber = count;

    }
    else{
        [UIApplication sharedApplication].applicationIconBadgeNumber = 0;        
    }
}
- (void)applicationWillEnterForeground:(UIApplication *)application
{
    /*
     Called as part of the transition from the background to the inactive state; here you can undo many of the changes made on entering the background.
     */
}

- (void)applicationDidBecomeActive:(UIApplication *)application
{
    /*
     Restart any tasks that were paused (or not yet started) while the application was inactive. If the application was previously in the background, optionally refresh the user interface.
     */
}

- (void)applicationWillTerminate:(UIApplication *)application
{
    /*
     Called when the application is about to terminate.
     Save data if appropriate.
     See also applicationDidEnterBackground:.
     */
}

@end
